package www.xpu.edu.app.job;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import www.xpu.edu.app.Activity_reg;
import www.xpu.edu.app.R;

public class Activity_job5 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job5);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        Button button=findViewById(R.id.title_change);
        Button button2=findViewById(R.id.title_back);

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        final EditText edit1=(EditText)findViewById(R.id.job5_edit1);
        final EditText edit2=(EditText)findViewById(R.id.job5_edit2);
        String num = getIntent().getStringExtra("data");
        TextView text=findViewById(R.id.title_text);
        text.setText("发送邀约");
        final String id = getIntent().getStringExtra("id");
        button.setText("发送");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendReguest(edit2.getText().toString().trim(),edit1.getText().toString().trim(),id);
                //sendReguest("010-12345678","欢迎你",String.valueOf(17));
                System.out.println(edit1.getText().toString()+"@"+edit2.getText().toString()+"@"+id);
              //  Toast.makeText(Activity_job5.this,"发布邀约功能出现玄学问题，尚无法修复，此功能敬请期待",Toast.LENGTH_LONG).show();
            }
        });
    }
    public void sendReguest(final String tel, final String context,final String id) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
                //        http://localhost:8080/xpu/reg1?num=19829088783&password=12345678

                try {
                   // System.out.println("测试->这个方法调用了1");
                    //局域网
                    //URL url = new URL("http://192.168.123.196:8080/xpu/reg1" + "?num=" + user + "&password=" + password);
                    //http://188.131.250.4:8080
                    //参数 tel,context,id,id可以传字符串类型，会自动转换
                    //URL url = new URL("http://188.131.250.4:8080/xpu/Updata_invite?tel=" + tel + "&context=" + context+"&id="+id);
                    //2019.04.30 03:19找到了解决方案，需要将汉字就行编码

                     /*
                    通过java提供的方法(在java.net包)加密和解密
                    String mytext = java.net.URLEncoder.encode("中国", "utf-8");
                    String mytext2 = java.net.URLDecoder.decode(mytext, "utf-8");
                    输出结果:
                    mytext: %E4%B8%AD%E5%9B%BD
                    mytex2: 中国
                    */
                    String  context2=context;
                     String context3=java.net.URLEncoder.encode(context2,"utf-8");


                    URL url = new URL("http://188.131.250.4:8080/xpu/Updata_invite?tel="+tel+"&context="+context3+"&id="+id);
                    //System.out.println("测试->这个方法调用了2");
                    connection = (HttpURLConnection) url.openConnection();
                   //System.out.println("测试->这个方法调用了3");
                    //connection.setRequestMethod("GET");
                    connection.setRequestMethod("POST");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    InputStream ips = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(ips));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    System.out.println(response.toString());
                    showResponse(response.toString());
                    /*if (!response.toString().trim().equals("0"))
                        startActivity(new Intent(Activity_reg.this, Activity_change.class));*/

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void showResponse(final String response) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(response.trim().equals("1"))
                    Toast.makeText(Activity_job5.this,"邀约发送成功", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(Activity_job5.this,"邀约发送失败", Toast.LENGTH_LONG).show();
                    //Toast.makeText(Activity_job5.this, response, Toast.LENGTH_LONG).show();
                //System.out.println(response);
            }
        });
    }
}
